package cn.bdqn.web;

import cn.bdqn.service.AirService;
import cn.bdqn.service.impl.AirServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(value = "/air/delete")
public class AirDeleteServlet extends HttpServlet {
    /*
    6. 删除一条空气质量信息.
    路径:
        GET 	http://localhost/air/delete
    参数:
        id = integer(必传项)
    响应:
        1. 完成删除操作.
        2. 重定向到首页.
     */

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 接收参数.
        String idStr = req.getParameter("id");
        //2. 封装数据
        Integer id = null;
        if(idStr != null){
            id = Integer.parseInt(idStr);
        }
        //3. 完成删除操作.
        AirService airService = new AirServiceImpl();
        Integer count = airService.deleteById(id);
        //4. 重定向到首页.
        if(count == 1){
            resp.sendRedirect(req.getContextPath() + "/index");
        }
    }
}
